package com.car.carcontrol;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.car.carcontrol.base.BaseFragment;
import com.car.carcontrol.util.SharedPreferencesUrls;
import com.google.gson.Gson;

import java.util.ArrayList;

import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;

//电器
public class Fragment_Seventh extends BaseFragment {
    private FrameLayout v;
    private Context context;

    private GifImageView iv_route;
    private GifDrawable gifDrawable;


    private long loopTime = 1000;  //时间显示间隔   默认100毫秒
    private boolean isThread = true;
    Thread thread;

    public Object[] allParam;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = new FrameLayout(getActivity());

        context = v.getContext();

        String json = SharedPreferencesUrls.getInstance().getString("allParam7", "");   //读取内部存储器：Fragment_Seventh界面控件参数字符串

        Log.e("fragment_Seventh===0", width+"==="+height+"==="+allParam);

        if(json==null || "".equals(json)){ //控件参数字符串为空显示默认控件
            allParam = new Object[]{
                    new Object[]{"tv", new Rect(609*width/1280-width_d, 334*height/480-height_d, 0, 0), "温度", 20, Color.WHITE, false, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}, R.drawable.btn_bcg},
            };
        }else{

            Gson gson = new Gson();
            allParam = gson.fromJson(json, Object[].class); //字符串转Object数组

            Log.e("fragment_Seventh===1", width+"==="+height+"==="+allParam.length+"==="+allParam[0]);

            initView();

            isThread = true;
            loopRead();
        }

        return v;
    }

    @Override
    public void onDestroy() {
        Log.e("f7===onDestroy", "===");

        isThread=false;

        if(thread!=null){
            thread.interrupt();
        }

        super.onDestroy();
    }

    private void initView(){
        for (int i=0; i<allParam.length; i++){
            Gson gson = new Gson();
            Object[] obj = gson.fromJson((String)allParam[i], Object[].class);

            Log.e("f7===obj", obj.length+"==="+obj);

            String type = (String)obj[0];
            Rect rect = gson.fromJson((String)obj[1], Rect.class);  //大小位置字符串转Rect对象

            Log.e("f7===obj1", type+"==="+rect);

            if("tv".equals(type)){
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                String name = (String)obj[2];
                int textSize = ((Double)obj[3]).intValue();  //字体大小double转int
                int textColor = ((Double)obj[4]).intValue();  //字体颜色double转int
                int textBcg = ((Double)obj[7]).intValue();  //字体背景double转int
                TextView tv = new TextView(context);
                tv.setText(name);
                tv.setTextSize(textSize);
                tv.setTextColor(textColor);
                tv.setBackgroundResource(textBcg);
                tv.setPadding(20*width/1280, 10*height/480, 20*width/1280, 10*height/480);
                v.addView(tv, params);


                boolean isCanClick = (boolean)obj[5];


                if(isCanClick){
                    tv.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Log.e("onClick===", v+"===");

                            String name = (String)obj[2];
                            byte[] bytes = (byte[])obj[4];

                            if("1".equals(name)){
                                iv_route.setImageResource(R.drawable.wind_1_gif);
                                gifDrawable.start();
                            }else if("2".equals(name)){
                                iv_route.setImageResource(R.drawable.wind_2_gif);
                                gifDrawable.start();
                            }else if("3".equals(name)){
                                iv_route.setImageResource(R.drawable.wind_3_gif);
                                gifDrawable.start();
                            }else if("4".equals(name)){
                                iv_route.setImageResource(R.drawable.wind_4_gif);
                                gifDrawable.start();
                            }else if("5".equals(name)){
                                iv_route.setImageResource(R.drawable.wind_5_gif);
                                gifDrawable.start();
                            }else if("6".equals(name)){
                                iv_route.setImageResource(R.drawable.wind_6_gif);
                                gifDrawable.start();
                            }else if("7".equals(name)){
                                iv_route.setImageResource(R.drawable.wind_7_gif);
                                gifDrawable.start();
                            }else if("AUTO".equals(name)){
                                iv_route.setImageResource(R.drawable.wind_auto_gif);
                                gifDrawable.start();
                            }

//                            write(bytes); //TODO
                        }
                    });

                    tv.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {


                            return false;
                        }
                    });
                }
            }else if("gif".equals(type)){
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(rect.right, rect.bottom);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                int imgSrc = (int)obj[2];
                iv_route = new GifImageView(context);
                iv_route.setImageResource(imgSrc);
                iv_route.setScaleType(ImageView.ScaleType.FIT_XY);
                gifDrawable = (GifDrawable) iv_route.getDrawable();
                gifDrawable.stop();

                v.addView(iv_route, params);
            }else if("iv".equals(type)){
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(rect.right, rect.bottom);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                int imgSrc = ((Double)obj[2]).intValue();  //图片资源id：double转int
                int imgSrc2 = ((Double)obj[3]).intValue();  //图片点击后资源id：double转int
                boolean isCanClick = (boolean)obj[4];   //图片能否点击
                byte[] bytes = gson.fromJson((String)obj[5], byte[].class);   //图片点击发送的串口指令：字符串转byte[]
                ImageView iv = new ImageView(context);
                iv.setImageResource(imgSrc);
                iv.setScaleType(ImageView.ScaleType.FIT_XY);
                v.addView(iv, params);

                if(isCanClick){
                    iv.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            Log.e("onTouch===", v+"==="+event.getAction()+"==="+bytes[0]+"==="+bytes[1]+"==="+bytes[2]);

                            if(event.getAction()==1){
                                iv.setImageResource(imgSrc);

                            }else{
                                iv.setImageResource(imgSrc2);

                            }

//                            write(bytes); //TODO

                            return true;
                        }
                    });

                }

            }
        }



    }

    private void loopRead(){
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (isThread){
                    try {
                        Thread.sleep(loopTime);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    if(MainActivity.curPos==6){
                        m_myHandler.sendEmptyMessage(1);
                    }


                }

            }
        });
        thread.start();
    }

    protected Handler m_myHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message mes) {
            switch (mes.what) {
                case 0:
                    break;

                case 1:
//                    read(cmd_needle); //TODO

//                    Log.e("f7===", "===");

                    break;


                default:
                    break;
            }
            return false;
        }
    });

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

}

